//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({
    data: {
        showNav: true, // 快捷导航是否展开
        testarr: [1, 2, 3, 4, 5],
        citycode: "bj",
        city: '北京',
        province: '北京',
        pindex: 0,
        cindex: 0,
        cityPick: ["选择城市"],
        // carType: ["燃油车", "新能源车", "混动车"],
        carType: [],
        carIndex: 0,
        category: "燃油车",
        localname: "本地",
        loaclArr: [{
            name: "本地",
            select: true
        }, {
            name: "外地",
            select: false
        }],
        subscribeText: '限行新消息提醒'
    },
    onLoad: function (options) {
        util.reportMessage("fangwenliang", {
            action: "限行查询"
        });
        // 监听页面加载的生命周期函数
        util.getUrl(app.globalData.url).then((res) => {
            this.getProvinceData(options.city, this.data.category);
        })
    },
    // 订阅成功
    subsuccess() {
        this.setData({
            subscribeText: "已订阅"
        })
    },
    // 获取定位
    getLocation() {
        qqmapsdk.reverseGeocoder({
            success: (res) => {
                let city = res.result.address_component.city;
                // let city="芜湖市";
                let citydata = this.data.citylist.filter(v => {
                    if (city.indexOf(v.cityname) != -1) {
                        return v;
                    }
                });
                if (citydata != '' && city.indexOf(this.data.city) == -1) {
                    ks.showModal({
                        content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '限行查询？',
                        success: res => {
                            if (res.confirm) {
                                this.setData({
                                    city: Array.isArray(citydata) ? citydata[0].cityname : this.data.city,
                                    citycode: Array.isArray(citydata) ? citydata[0].citycode : this.data.citycode,
                                    province: Array.isArray(citydata) ? citydata[0].province : this.data.province,
                                    cityPick: ["选择城市"]
                                })
                                
                                util.checkSubscribe("限行", this.data.city).then(result => this.setData({
                                    subscribeText: result ? '已订阅' : '限行新消息提醒'
                                }))
                                let clist = [];
                                this.data.provPick.forEach((v, k) => {
                                    if (v == this.data.province) {
                                        this.setData({
                                            pindex: k,
                                        })
                                    }
                                })
                                this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                                    clist.push(v.cityname)
                                })
                                this.setData({
                                    cityPick: this.data.cityPick.concat(clist)
                                })
                                this.data.cityPick.forEach((v, k) => {
                                    if (v == this.data.city) {
                                        this.setData({
                                            cindex: k,
                                        })
                                    }
                                })
                                // 获取城市公众号二维码
                                this.getQrcode(this.data.citycode);
                                this.getCategory(this.data.citycode);
                                this.getInfoData(this.data.citycode, this.data.category);

                            }
                        }
                    })
                }

            },
        });
    },

    // 获取城市公众号二维码
    getQrcode(citycode) {
        util.cityConfig(citycode).then(res => {
            this.setData({
                qrcodeimg: res.qrcode,
            })
        })
    },

    // 获取有隔离政策的城市
    getProvinceData(optcode) {
        let data = {
            action: "xianxingchaxun",
            type: "city",
        };
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data.city) {
                this.setData({
                    cityArray: res.data.data.city,
                })
                let provPick = [];
                let citylist = [];
                let clist = [];
                this.data.cityArray.forEach(v => {
                    v.citylist.forEach(v => {
                        citylist.push(v)
                    })
                    provPick.push(v.province)
                    this.setData({
                        provPick,
                        citylist
                    })
                })
                if (this.data.citylist) {
                    this.data.citylist.forEach((v, k) => {
                        if (optcode == v.citycode) {
                            this.setData({
                                citycode: v.citycode,
                                city: v.cityname,
                                province: v.province,
                            })
                        } else {
                            this.setData({
                                citycode: this.data.citycode,
                                city: this.data.city,
                                province: this.data.province,
                            })
                        }
                    })
                }

                this.data.provPick.forEach((v, k) => {
                    if (v == this.data.province) {
                        this.setData({
                            pindex: k,
                        })
                    }
                })

                this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                    clist.push(v.cityname)
                })
                this.setData({
                    cityPick: this.data.cityPick.concat(clist)
                })

                this.data.cityPick.forEach((v, k) => {
                    if (v == this.data.city) {
                        this.setData({
                            cindex: k,
                        })
                    }
                })
                // 获取城市公众号二维码
                this.getQrcode(this.data.citycode);
                this.getCategory(this.data.citycode);
                this.getInfoData(this.data.citycode, this.data.category);
                util.checkSubscribe("限行", this.data.city).then(result => this.setData({
                    subscribeText: result ? '已订阅' : '限行新消息提醒'
                }))
             
                if (this.data.citylist != "" && Array.isArray(this.data.citylist)) {
                    // 获取定位
                    this.getLocation();
                }
            }

        })
    },

    getCategory(citycode) {
        let data = {
            action: "category",
            citycode: citycode,
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.setData({
                carType: res.data.data,
            })
        })
    },

    // 获取信息
    getInfoData(citycode, category) {
        let data = {
            action: "xianxingchaxun",
            citycode: citycode ? citycode : this.data.citycode,
            category: category,
        };
        util.request('zhuanti.php', data, ).then(res => {
            ks.setNavigationBarTitle({
                title: res.data.data ? res.data.data.title : '限行查询'
            })
            if (res.data.data.daofen) {
                res.data.data.daofen.content = res.data.data.daofen.content
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                    .replace(/<p>/ig, '<p class="p_class">')
                    .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                    .replace(/<img/gi, '<img style="width:100%;height:auto"')
            }

            this.setData({
                infoData: res.data.data,
                limitData: res.data.data.xianxing != '' ? res.data.data.xianxing.item[0] : '',
            })

            let limitInfor = [];
            if (this.data.limitData.display)
                limitInfor = this.data.limitData.display.limitinfo.filter(v => {
                    if (v.tab == "本地") {
                        return v;
                    }
                })
            this.setData({
                limitInfor,
            })
        })
    },

    // 省份选择
    bindProv: function (e) {
        this.setData({
            pindex: e.detail.value,
            cindex: 0,
            cityPick: ["选择城市"],
        })
        let clist = [];
        this.data.cityArray[this.data.pindex].citylist.forEach(v => {
            clist.push(v.cityname)
        })
        this.setData({
            cityPick: this.data.cityPick.concat(clist),
        })
    },

    // 城市选择
    bindCity(e) {
        this.setData({
            cindex: e.detail.value
        })
        let cityname = this.data.cityPick[this.data.cindex];
        if (this.data.citylist)
            this.data.citylist.forEach(v => {
                if (v.cityname == cityname) {
                    let citycode = v.citycode;
                    this.setData({
                        citycode,
                        city: cityname
                    })
                    util.checkSubscribe("限行", this.data.city).then(result => this.setData({
                        subscribeText: result ? '已订阅' : '限行新消息提醒'
                    }))
                }
            })
        // 获取城市公众号二维码
        this.getQrcode(this.data.citycode);
        this.getCategory(this.data.citycode);
        this.getInfoData(this.data.citycode, this.data.category);
        this.data.loaclArr.forEach(v => {
            if (v.name == "本地") {
                v.select = true;
            } else {
                v.select = false;
            }
        })
        this.setData({
            loaclArr: this.data.loaclArr,
        })
    },

    // 汽车类型
    bindSelect(e) {
        let carIndex = e.detail.value;

        this.setData({
            carIndex,
            category: this.data.carType[carIndex],
        })
        this.getInfoData(this.data.citycode, this.data.category);
    },

    loaclClick(e) {
        let name = e.currentTarget.dataset.name;

        this.data.loaclArr.forEach(v => {
            if (v.name == name) {
                v.select = true;
            } else {
                v.select = false;
            }
        })
        this.setData({
            loaclArr: this.data.loaclArr,
            localname: name,
        })
        let limitInfor = [];
        if (this.data.limitData && this.data.limitData.display)
            limitInfor = this.data.limitData.display.limitinfo.filter(v => {
                if (v.tab == name)
                    return v;
            });
        this.setData({
            limitInfor,
        })

    },

    navDetail(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        let url = e.currentTarget.dataset.url;
        let citycode = e.currentTarget.dataset.citycode;
        if (id != 0) {
            ks.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + citycode,
            })
        } else {
            if (url != "") {
                if (url.indexOf("pages") == -1) {
                    ks.navigateTo({
                        url: '/pages/webview/webview?path=' + encodeURIComponent(url),
                    })
                } else {
                    ks.navigateTo({
                        url: "/" + url,
                    })
                }
            } else {
                ks.showToast({
                    title: "暂无数据，敬请期待！",
                    icon: "none",
                })
            }
        }
    },

    onShareAppMessage: function () {
        // 用户点击右上角转发
        return {
            title: this.data.infoData ? this.data.infoData.title : "限行查询",
        }
    }
});